package cf.huzpsb.anticheat.detections.movement;
//this class needs patches badly.-hs

import cf.huzpsb.anticheat.detections.Flag;
import cf.huzpsb.anticheat.management.Main;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerVelocityEvent;
import org.bukkit.potion.PotionEffectType;

import java.util.ArrayList;
import java.util.Iterator;

public class Speed implements Listener {
    Main main;
    Player player;
    Flag playerProfile;
    String playerName;
    int airTime;
    int speedBuffer;
    int liquidBuffer;
    int iceBuffer;
    int airBuffer;
    int differenceBuffer;
    int boostBuffer;
    int jumpBuffer;
    int vineBuffer;
    int velocityBuffer;
    int denyBuffer;
    int airBoostBuffer;
    double lastDistance = 0.0D;
    double lastYOffset = 0.0D;
    boolean up = false;

    public Speed(Main main, Player player, Flag playerProfile) {
        main.getServer().getPluginManager().registerEvents(this, main);
        this.main = main;
        this.player = player;
        this.playerProfile = playerProfile;
        this.playerName = player.getName();
    }

    @EventHandler
    public void playerMoveEvent(PlayerMoveEvent e) {
        if (e.getPlayer().getName().equals(this.playerName)) {
            this.player = e.getPlayer();
            float maxDistance = 0.4F;
            if (this.player.getGameMode() == GameMode.CREATIVE || this.player.getGameMode() == GameMode.SPECTATOR) {
                return;
            }

            if (this.player.getFallDistance() > 1.0F) {
                return;
            }

            if (this.jumpBuffer > 0) {
                --this.jumpBuffer;
            }

            if (this.vineBuffer > 0) {
                --this.vineBuffer;
                return;
            }

            if (this.velocityBuffer > 0) {
                --this.velocityBuffer;
                this.playerProfile.damageBuffer = 10;
            }

            if (this.player.hasPotionEffect(PotionEffectType.SPEED)) {
                maxDistance *= 2.0F;
            }

            double yOffset = e.getFrom().getY() - e.getTo().getY();
            double distance = e.getFrom().distance(e.getTo());
            Location playerLoc = this.player.getLocation();
            Location playerFeetLoc = new Location(this.player.getWorld(), playerLoc.getX(), playerLoc.getY() - 1.0D, playerLoc.getZ(), playerLoc.getYaw(), playerLoc.getPitch());
            Location playerHeadLoc = new Location(this.player.getWorld(), playerLoc.getX(), playerLoc.getY() + 2.0D, playerLoc.getZ(), playerLoc.getYaw(), playerLoc.getPitch());
            boolean inAir = this.main.isInAir(this.player, playerLoc);
            String blockMat = playerFeetLoc.getBlock().getType().toString();
            if (playerFeetLoc.getBlock().getType() == Material.SLIME_BLOCK) {
                this.playerProfile.inSlimeJump = true;
                this.playerProfile.slimeEnterFallDistance = this.player.getFallDistance();
            }

            if (this.playerProfile.damageBuffer == 0 && !inAir && distance > 0.800000011920929D && !this.playerProfile.inSlimeJump && !blockMat.contains("water") && !blockMat.contains("lava")) {
                e.setCancelled(true);
            }

            if (distance > 3.0D) {
                this.playerProfile.flag("movement.ground.teleport", 10);
            }

            if (playerHeadLoc.getBlock().getType().isSolid()) {
                this.speedBuffer = -10;
                return;
            }

            if (playerLoc.getBlock().getType() == Material.VINE) {
                this.vineBuffer = 10;
            }

            if (yOffset <= -0.4000000059604645D) {
                this.up = true;
            }

            if (yOffset > -0.4000000059604645D && yOffset < 0.0D) {
                this.up = false;
            }

            if (yOffset > 0.0D && yOffset < 0.20000000298023224D && this.up) {
                ++this.denyBuffer;
                if (this.denyBuffer >= 10) {
                    this.playerProfile.flag("movement.jump.deny", 1);
                }
            } else if (this.denyBuffer > 0) {
                --this.denyBuffer;
            }

            if (this.playerProfile.damageBuffer > 0 || this.playerProfile.joinBuffer > 0) {
                maxDistance *= 2.0F;
            }

            if (playerFeetLoc.getBlock().getType() == Material.WATER || playerFeetLoc.getBlock().getType() == Material.STATIONARY_WATER || playerFeetLoc.getBlock().getType() == Material.LAVA || playerFeetLoc.getBlock().getType() == Material.STATIONARY_LAVA) {
                this.liquidBuffer = 30;
            }

            if (this.liquidBuffer > 0) {
                --this.liquidBuffer;
                maxDistance *= 2.0F;
            }

            Material groundMat = playerFeetLoc.getBlock().getType();
            ArrayList<Material> iceTypes = new ArrayList();
            iceTypes.add(Material.ICE);
            iceTypes.add(Material.PACKED_ICE);
            Iterator var15 = iceTypes.iterator();

            while (var15.hasNext()) {
                Material ice = (Material) var15.next();
                if (groundMat == ice) {
                    this.iceBuffer = 30;
                }
            }

            if (yOffset < 0.0D) {
                this.lastYOffset = yOffset;
            }

            double offsetDifference = yOffset - this.lastYOffset;
            if (offsetDifference > 0.66 && this.liquidBuffer == 0 && this.playerProfile.damageBuffer == 0) {
                ++this.differenceBuffer;
                if (this.differenceBuffer > 4) {
                    this.playerProfile.flag("movement.jump.difference." + offsetDifference, 1);
                }

                this.lastYOffset = yOffset;
            }//here.0.5?

            if (this.iceBuffer > 0) {
                maxDistance = 0.7F;
                --this.iceBuffer;
            }

            if (yOffset > 0.0D && yOffset < 0.009999999776482582D && this.playerProfile.damageBuffer == 0 && playerLoc.getBlock().getType().isSolid()) {
                this.playerProfile.flag("movement.jump.mini", 1);
            }

            float continuousMaxDistance = 0.3F;
            if (this.player.hasPotionEffect(PotionEffectType.SPEED)) {
                continuousMaxDistance = 0.4F;
            }

            if (distance >= (double) continuousMaxDistance && this.jumpBuffer == 0) {
                this.boostBuffer += 2;
                if (this.boostBuffer > 20) {
                    this.playerProfile.flag("movement.ground.speed.continuous", 1);
                }
            }

            if (!inAir && distance >= 0.5D && this.speedBuffer >= 0 && this.playerProfile.damageBuffer == 0) {
                this.airBoostBuffer += 1;
                if (this.airBoostBuffer > 2) {
                    this.playerProfile.flag("movement.air.speed.continuous", 1);
                }
            }

            if (this.boostBuffer > 0) {
                --this.boostBuffer;
            }

            if (this.airBoostBuffer > 0) {
                --this.airBoostBuffer;
            }

            if (yOffset != 0.0D) {
                this.jumpBuffer = 20;
            }

            if (this.airTime == 0) {
                this.airBuffer = 0;
                if (!inAir && yOffset > -0.5D && distance >= (double) maxDistance) {
                    ++this.speedBuffer;
                    if (this.speedBuffer >= 10) {
                        this.playerProfile.flag("movement.ground.speed.instant", 1);
                    }
                } else if (this.speedBuffer > 0) {
                    this.speedBuffer = 0;
                } else if (this.speedBuffer < 0) {
                    ++this.speedBuffer;
                }
            } else {
                --this.airTime;
                if (this.player.getFallDistance() <= 1.0F && distance >= 0.5D && this.playerProfile.damageBuffer == 0 && this.speedBuffer >= 0) {
                    ++this.airBuffer;
                    if (this.airBuffer > 5) {
                        this.playerProfile.flag("movement.jump.speed", 1);
                    }
                } else if (this.airBuffer > 0) {
                    --this.airBuffer;
                }
            }

            if (inAir) {
                this.airTime = 3;
            }

            this.lastDistance = distance;
        }

    }

    @EventHandler
    public void onPlayerVelocity(PlayerVelocityEvent e) {
        if (e.getPlayer().getName().equals(this.playerName) && e.getVelocity().getY() >= 0.800000011920929D) {
            this.player = e.getPlayer();
            this.velocityBuffer = 10;
        }

    }
}
